package com.ly.hotel.view.floatWindows;

import java.lang.ref.WeakReference;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;

import com.ly.hotel.R;
import com.ly.hotel.common.Constants;
import com.ly.hotel.view.common.CustomDialog;

public class FloatToolsDialog extends Service {
	// 定义浮动窗口布局
//	LinearLayout mFloatLayout;
	private LinearLayout mFloatLayout;
	LayoutParams wmParams;
	// 创建浮动窗口设置布局参数的对象
	WindowManager mWindowManager;

//	ImageButton mFloatView;
	private ThisHandler handler;
	CustomDialog.Builder builder;
	CustomDialog.Builder dialog;
	View view;
	Context context;
	@Override
	public void onCreate() {
		super.onCreate();
		context = getBaseContext();
		builder = new CustomDialog.Builder(this);
		dialog = new CustomDialog.Builder(this);
		
		handler = new ThisHandler(this);
		createFloatView();
		
//		registerReceiver(bottomMenuRev, new IntentFilter(Constants.Broadcast.SHUAKA_TIPS));
		
//		sendBroadcast(new Intent(Constants.Broadcast.SHUAKA_TIPS).putExtra("flag", Constants.CLOSE_TIPS));
	}


	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	private void createFloatView() {
		wmParams = new LayoutParams();
		getApplication();
		// 获取WindowManagerImpl.CompatModeWrapper
		mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);

		DisplayMetrics dm = new DisplayMetrics();
		mWindowManager.getDefaultDisplay().getMetrics(dm);

		// 设置window type
		wmParams.type = LayoutParams.TYPE_PHONE;
		// 设置图片格式，效果为背景透明
		wmParams.format = PixelFormat.RGBA_8888;
		// 设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
		wmParams.flags = 
		// LayoutParams.FLAG_NOT_TOUCH_MODAL |
		LayoutParams.FLAG_NOT_FOCUSABLE
		// LayoutParams.FLAG_NOT_TOUCHABLE
		;

		// 调整悬浮窗显示的停靠位置为左侧置顶
		wmParams.gravity = Gravity.CENTER | Gravity.RIGHT;
 
		// 设置悬浮窗口长宽数据
		wmParams.width = 80;
		wmParams.height = LayoutParams.WRAP_CONTENT;

		LayoutInflater inflater = LayoutInflater.from(getApplication());
		// 获取浮动窗口视图所在布局
		mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_tools_layout, null);
		
		// 添加mFloatLayout
		mWindowManager.addView(mFloatLayout, wmParams);
		
		mFloatLayout.setVisibility(View.VISIBLE);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}


	/* 这个就是是否显示浮动小圆点的事件接口 */
	BroadcastReceiver bottomMenuRev = new BroadcastReceiver() {
		@Override
		public void onReceive(Context cont, Intent inte) {
			int flag = inte.getIntExtra("flag", 0);
			if (flag == Constants.SHOW_TIPS) {// 显示
				handler.post(new Runnable() {

					@Override
					public void run() {
//						gf.setVisibility(View.VISIBLE);
//						if(handler.hasMessages(0)){
//							handler.removeMessages(0);
//						}
//						handler.sendEmptyMessageDelayed(0, Integer.parseInt(SysConf.extendConfGetValue(SysConf.SYS_CONF_SHUAKA_TIPS_SHOW_TIME,"4000")));
					}
				});
			} else if (flag == Constants.CLOSE_TIPS) {
				handler.post(new Runnable() { 

					@Override
					public void run() {
//						gf.setVisibility(View.INVISIBLE);
					}
				});
			}
		}
	};
	
	private static class ThisHandler extends Handler {
		private WeakReference<FloatToolsDialog> wrActivity;

		public ThisHandler(FloatToolsDialog activity) {
			wrActivity = new WeakReference<FloatToolsDialog>(activity);
		}

		@Override
		public void handleMessage(Message message) {
			FloatToolsDialog act = wrActivity.get();
			if (act != null) {
				// 这个是通过事实消息接口获取的弹窗信息打开的
				if (message.what == 0) {
//					act.sendBroadcast(new Intent(Constants.Broadcast.SHUAKA_TIPS).putExtra("flag", Constants.CLOSE_TIPS));
				}
			}
		}
	}

}
